package com.xiaouyudeguang.common.respose;

import com.xiaouyudeguang.common.exception.BusinessException;
import com.xiaouyudeguang.common.utils.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResultDto<V> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回码（0表示正常；-1表示系统异常；大于1表示业务异常）
     */
    private long code;

    /**
     * 数据
     */
    private V data;

    /**
     * 消息内容
     */
    private String msg;

    @Override
    public String toString() {
        return JsonObject.toJson(this);
    }

    public static <T> List<T> getPageRecords(ResultDto<?> resultDto, Class<T> returnType) {
        if (resultDto == null || resultDto.getCode() != 0) {
            return null;
        }
        PageDto<?> pageDto = JsonObject.fromJson(resultDto.getData(), PageDto.class);
        List<?> content = pageDto.getContent();
        if (content == null) {
            return null;
        }
        List<T> dataList = new ArrayList<>();
        for (Object obj : content) {
            dataList.add(JsonObject.fromJson(obj, returnType));
        }
        return dataList;
    }

    public static <T> T getResult(ResultDto<T> resultDto) throws Exception {
        if (resultDto == null) {
            throw new BusinessException("无操作结果");
        }
        if (resultDto.getCode() != 0) {
            throw new BusinessException(resultDto.getMsg());
        }
        return resultDto.getData();
    }

    public static <T> ResultDto<T> result(long code, T data) {
        return result(code, data, null);
    }

    public static <T> ResultDto<T> result(long code, T data, String message) {
        ResultDto<T> resultDto = new ResultDto<>();
        resultDto.setCode(code);
        resultDto.setData(data);
        resultDto.setMsg(message);
        return resultDto;
    }

    public static <T> ResultDto<T> err() {
        return result(-1, null, "发生错误(-1),请稍后重试");
    }

    public static <T> ResultDto<Object> err(String msg) {
        return result(System.currentTimeMillis(), null, msg);
    }

    public static <T> ResultDto<Object> err(long code, String msg) {
        return result(code, null, msg);
    }

    public static <T> ResultDto<T> failure() {
        return result(1, null, "失败");
    }

    public static <T> ResultDto<T> failure(String msg) {
        return result(1, null, msg);
    }

    public static <T> ResultDto<T> failure(long code, String msg) {
        return result(code, null, msg);
    }

    public static <T> ResultDto<T> ok() {
        return result(0, null, "成功");
    }

    public static <T> ResultDto<T> ok(T data) {
        return result(0, data, "成功");
    }

    public static ResultDto<Boolean> ok(Boolean result) {
        return result(result ? 0 : 1, result, result ? "成功" : "失败");
    }
}